package com.gjy.util.sb.advice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-10-23 17:11:28
 */
@RestControllerAdvice
@Order(99)
public class ResponseBodyAdviceImpl2 implements ResponseBodyAdvice<Object> {

    private static final Logger log = LoggerFactory.getLogger(ResponseBodyAdviceImpl2.class);

    @Override
    public boolean supports(MethodParameter parameter, Class<? extends HttpMessageConverter<?>> cls) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object result, MethodParameter parameter, MediaType type, Class<? extends HttpMessageConverter<?>> cls, ServerHttpRequest request, ServerHttpResponse response) {
        log.info("rrr: {}", result);
        if (response instanceof ResponseEntity) {
            log.error("{}", result);
        }
        return result;
    }

}
